//生成菜单
var menuItem = Vue.extend({
    name: 'menu-item',
    props: {item: {}, index: 0},
    template: [
        '<li :class="{active: (item.level===1 && index === 0)}">',
        '<a v-if="item.level==1&&item.linkurl == null" href="javascript:;" class="dropdown-toggle">',
        '<i v-if="item.iconcss != null" :class="item.iconcss"></i>',
        '<span class="menu-text">',
        '<i class="icon-align-justify  bgicon"></i>{{item.name}}',
        '</span>',
        '<b class="arrow icon-angle-down"></b>',
        '</a>',
        '<ul v-if="item.level === 1" class="submenu">',
        '<menu-item :item="item" :index="index" v-for="(item, index) in item.twolevelmenulist"></menu-item>',
        '</ul>',
        '<a v-if="item.level==1&&item.linkurl != null" :href="\'#\'+item.linkurl">',
        '<i v-if="item.iconcss != null" :class="item.iconcss"></i>',
        '<span class="menu-text">{{item.name}}</span>',
        '</a>',
        '<a v-if="item.level>1&&item.linkurl != null" :href="\'#\'+item.linkurl">',
        '<i v-if="item.iconcss != null" :class="item.iconcss"></i>',
        '<i v-else class="icon-circle "></i> {{item.name}}',
        '</a>',
        '</li>'
    ].join('')
});

//iframe自适应
$(window).on('resize', function () {
    var $content = $('.page-content');
    $content.height($(this).height() - 120);
    $content.find('iframe').each(function () {
        $(this).height($content.height());
    });
}).resize();

//注册菜单组件
Vue.component('menuItem', menuItem);

var vm = new Vue({
    el: '#jkapp',
    data: {
        user: {},
        menuList: {},
        main: "main.html",
        password: '',
        newPassword: '',
        navTitle: "欢迎页"
    },
    methods: {
        getMenuList: function () {
            $.getJSON(baseURL + "webusermenu/querywebusermenu", function (r) {
                vm.menuList = r.webUserMenuList;

            });
        }
    },
    watch: {
        // menuList，并自动执行下面的函数
        menuList: function (val, oldVal) {
            //路由
            var router = new Router();
            routerList(router, vm.menuList);
            router.start();
        }
    },
    created: function () {
        this.getMenuList();
        // this.getUser();
    }
});

function routerList(router, menuList) {
    for (var key in menuList) {
        var menu = menuList[key];
        if (menu.linkurl == null) {
            routerList(router, menu.twolevelmenulist);
        } else if (menu.linkurl != null) {
            router.add('#' + menu.linkurl, function () {
                var url = window.location.hash;
                //替换iframe的url
                vm.main = url.replace('#', '');

                //导航菜单展开
                // $(".treeview-menu li").removeClass("active");
                $(".sidebar-menu li").removeClass("active");
                $("a[href='" + url + "']").parents("li").addClass("active");

                vm.navTitle = $("a[href='" + url + "']").text();
            });
        }
    }
}

$(function () {
    var fatherBody = $(window.top.document.body);
    $.ajax({
        url: window.jkconfig.webapiurlv + "applicantmanage/list",
        data: {pageNumber: 1, pageSize: 15, areaId: 0},
        success: function (data) {
            if (data.code == 0) {
                $("#patent-count").html(data.list.totalItemsCount);
            }
        }
    })
    fatherBody.find(".login_userName").html(localStorage["userName"]);
    fatherBody.find("#userName").val(localStorage["userName"]);
    /*权利人管理*/
    $("body").on("click", ".btnEditObligeeConfirm", function () {
        var applicantId = fatherBody.find(".applicantName").attr("data-applicantId");
        var areaId = fatherBody.find("#mainAreaId").val();
        var industryIds = [];
        $.each(fatherBody.find("#modal-editObligee input[name=industryId]:checked"), function (index, element) {
            industryIds.push($(element).val());
        });
        industryIds = industryIds.join(",");
        $.ajax({
            url: window.jkconfig.webapiurlv + "applicantmanage/update",
            type: 'POST',
            data: {applicantId: applicantId, areaId: areaId, industryIds: industryIds},
            success: function (data) {
                if (data.code == 0) {
                    alert(data.msg);
                    window.location.reload();
                } else {
                    alert(data.msg);
                    window.location.reload();
                }
            }

        })
    }).on("click", ".loginOut", function () {
        $.ajax({
            url: window.jkconfig.webapiurlv + "logout",
            type: 'POST',
            success: function (data) {
                if (data.code == 0) {
                    localStorage.clear();
                    parent.location.href = "login.html";
                } else {
                    $.showErr(data.msg);
                }
            }
        })
    }).on("click", ".btnClose", function () {
        $(this).closest(".modal").hide();
        $(".modal-backdrop").hide();
    }).on("click", ".btnDeleteArea", function () {
        var id = fatherBody.find("#deletAreaModal .selectedArea").attr("data-id");
        $.ajax({
            url: window.jkconfig.webapiurlv + "typemanage/deletearea/" + id,
            type: "post",
            success: function (data) {
                fatherBody.find("#deletAreaModal").hide();
                if (data.code == 0) {
                    alert(data.msg);
                    setTimeout(function () {
                        window.parent.location.reload();
                    }, 1000);
                } else {
                    alert(data.msg);
                }
            }
        })
    }).on("click", ".btnDeleteIndustry", function () {
        var id = fatherBody.find("#deletIndustryModal .selectedIndustry").attr("data-id");
        $.ajax({
            url: window.jkconfig.webapiurlv + "typemanage/deleteindustry/" + id,
            type: "post",
            success: function (data) {
                fatherBody.find("#deletIndustryModal").hide();
                if (data.code == 0) {
                    //alert(data.msg);
                    templateFun();
                } else {
                    alert(data.msg);
                }
            }
        })
    }).on("click", ".btnIIndustryConfirm", function () {
        /*判断industryIds不为空*/
        var industryIds = [];
        $.each($("[name=industryId]:checked"), function (index, element) {
            industryIds.push($(element).val());
        })
        if (industryIds.join("") == "") {
            alert("请选择行业!");
            /*判断patId不为空*/
        } else if ($(window.frames[0].document).find("[name=checkItem]:checked").length < 1) {
            alert("请选择权利人！");
        } else {
            var applicantIds = [];
            var checkItems = $(window.frames[0].document).find("[name=checkItem]:checked");
            $.each(checkItems, function (index, element) {
                applicantIds.push($(element).attr("data-applicantId"));
            })
            industryIds = industryIds.join(",")
            applicantIds = applicantIds.join(",");
            $.ajax({
                url: window.jkconfig.webapiurlv + "applicantmanage/classifyindustry",
                type: 'POST',
                data: {industryIds: industryIds, applicantIds: applicantIds},
                success: function (data) {
                    if (data.code == 0) {
                        alert("归类成功!");
                        window.location.reload();

                    } else {
                        alert("归类失败!");
                        window.location.reload();
                    }
                }
            })
        }
    }).on("click", ".btnOAreaConfirm", function () {
        /*判断areaId不为空*/
        if ($("#OareaId").val() == "") {
            $.showErr("请选择区域!");
            /*判断patId不为空*/
        } else if ($(window.frames[0].document).find("[name=checkItem]:checked").length < 1) {
            $.showErr("请选择权利人！");
        } else {
            var areaId = $("#OareaId").val();
            var applicantIds = [];
            var checkItems = $(window.frames[0].document).find("[name=checkItem]:checked");
            $.each(checkItems, function (index, element) {
                applicantIds.push($(element).attr("data-applicantId"));
            })
            applicantIds = applicantIds.join(",");
            $.ajax({
                url: window.jkconfig.webapiurlv + "applicantmanage/classifyarea",
                type: 'POST',
                data: {areaId: areaId, applicantIds: applicantIds},
                success: function (data) {
                    if (data.code == 0) {
                        alert("归类成功!");
                        window.location.reload();

                    } else {
                        alert("归类失败!");
                        window.location.reload();
                    }
                }
            })
        }
    }).on("click", ".btnConfirm", function () {
        /*判断areaId不为空*/
        if ($("#areaId").val() == "") {
            alert("请选择区域!");
            /*判断patId不为空*/
        } else if ($(window.frames[0].document).find("[name=checkItem]:checked").length < 1) {
            alert("请选择申请专利");
        } else {
            var areaId = $("#areaId").val();
            var patId = [];
            var checkItems = $(window.frames[0].document).find("[name=checkItem]:checked");
            $.each(checkItems, function (index, element) {
                patId.push($(element).attr("data-patId"));
            })
            patId.join("");
            $.ajax({
                url: window.jkconfig.webapiurlv + "analysis/patentApply/classify?areaId=" + areaId + "&patId=" + patId,
                success: function (data) {
                    if (data.code == 0) {
                        alert("归类成功!");
                        window.location.reload();
                    } else {
                        alert("归类失败!");
                    }
                }
            })
        }
    }).on("click", ".btnDeal", function () {
        localStorage["areaId"] = $(this).attr("data-areaId");
        window.parent.location = "/#system/obligeeManage.html";
    })

    function templateFun() {
        $.ajax({
            url: window.jkconfig.webapiurlv + "typemanage/pagequerypacindustry",
            data: {pageSize: 15, pageNum: 1},
            success: function (data) {
                if (data.code == 0) {
                    var html = template('contentTemplate', data);
                    $("#mainurl").contents().find("#Js_page_industrycontent").html(html);
                    alert("删除成功!");
                }
            }
        })
    }
})







